layui.use(['form', 'layer', 'table', 'laytpl', 'util'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table,
        util = layui.util;
    //用户列表
    var tableIns = table.render({
        elem: '#list',
        url: '/antiFakeSys/antiFakeCode/search.do',
        page: true,
        limits: [10, 15, 20, 25],
        limit: 20,
        id: "listTable",
        cols: [[
            {type: 'checkbox'},
            {field: 'id', title: 'id', align: "center"},
            {field: 'name', title: '防伪码名称', align: "center"},
            {field: 'code', title: '防伪码', align: "center"},
            {
                field: 'firstSearchTime', title: '第一次查询时间', align: "center", templet: function (d) {
                    if(d.firstSearchTime){
                        return util.toDateString(d.firstSearchTime, 'yyyy-MM-dd HH:mm:ss');
                    }else {
                        return "";
                    }
                }
            },
            {field: 'searchNum', title: '查询次数', align: "center"},
            {
                field: 'validateTime', title: '上传日期', align: "center", templet: function (d) {
                   if(d.validateTime){
                       return util.toDateString(d.validateTime, 'yyyy-MM-dd');
                   }else {
                       return "";
                   }
                }
            },
            {
                field: 'status', title: '状态', align: "center", templet: function (d) {
                    if(d.status==0){
                        return "<label style='color: red'>未上传</label> ";
                    }else {
                        return "<label style='color: #10ff1c'>已上传</label> ";
                    }
                }
            },
            {
                field: 'createTime', title: '创建时间', align: "center", templet: function (d) {
                    return util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss');
                }
            },
            {title: '操作', templet: '#listBar', minWidth: 200, fixed: "right", align: "center"}
        ]]
    });
    $("#search_btn").on("click", function () {
        table.reload("listTable", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                id: $("#id").val(),
                name: $("#name").val(),
                code: $("#code").val(),
            }
        })
    });

    function add(edit) {
        var url = "edit.html";
        if (edit) {
            url = "edit.html?id=" + edit.id;
        }
        var index = layui.layer.open({
            title: "修改/添加防伪码",
            type: 2,
            content: url,
            success: function (layero, index) {
                setTimeout(function () {
                    layui.layer.tips('点击此处返回列表页', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        });
        layui.layer.full(index);
    };
    function generate() {
        layer.prompt({
            title: '请输入该批次防伪码名称和数量'
            ,
            content: "<input type='text' id='name' placeholder='请输入该批次防伪码名称(必填)' class='layui-input'/><br><input type='text' id='num' placeholder='请输入要添加的数量(必填)' class='layui-input'/>"
            ,
            area: ['300px', '100px'],
            yes: function (index, layero) {
                //防伪码名称
                var name = layero.find("#name").val();
                //生成数量
                var num = layero.find("#num").val();
                $.get("/antiFakeSys/antiFakeCode/generate.do", {
                    name: name, num: num
                }, function (res) {
                    if (res.code == 0) {
                        table.reload("listTable", {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        })
                        layer.close(index);
                    } else {
                        layer.msg(res.msg);
                    }
                });
            }
        });
    };

    $("#addNews_btn").click(function () {
        generate();
    });

    function audit(ids, status) {
        $.get("/antiFakeSys/antiFakeCode/updateStatus.do", {ids: ids, status: status}, function (res) {
            if (res.code == 0) {
                table.reload("listTable", {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    where: {
                        id:
                            $("#id").val(),
                        name:
                            $("#name").val(),
                        code:
                            $("#code").val(),
                    }
                })
            } else {
                layer.msg("操作失败。");
            }
        });
    };

    function getCheckIds() {
        var checkStatus = table.checkStatus('listTable')
            , data = checkStatus.data;
        var ids = "";
        $.each(data, function (index, antiFakeCode) {
            if (index == 0) {
                ids = antiFakeCode.id;
            } else {
                ids = ids + "," + antiFakeCode.id;
            }
        });
        return ids;
    };
    $("#pass_btn").click(function () {
        var ids = getCheckIds();
        if (ids == "") {
            layer.msg("请选择要操作的数据。");
        } else {
            audit(ids, 1);
        }
    });

    $("#notPass_btn").click(function () {
        var ids = getCheckIds();
        if (ids == "") {
            layer.msg("请选择要操作的数据。");
        } else {
            layer.confirm('确定不通过？', {icon: 3, title: '提示信息'}, function (index) {
                audit(ids, 0);
                layer.close(index);
            })
        }
    });
    //列表操作
    table.on('tool(list)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;
        if (layEvent === 'edit') { //编辑
            add(data);
        } else if (layEvent === 'clearNum') {
            $.get("/antiFakeSys/antiFakeCode/clearNum.do", {id: data.id}, function (res) {
                if (res.code == 0) {
                    table.reload("listTable", {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        },
                        where: {
                            id:
                                $("#id").val(),
                            name:
                                $("#name").val(),
                            code:
                                $("#code").val(),
                        }
                    })
                } else {
                    layer.msg("操作失败。");
                }
            });
        }
    });
});